package com.yfp.operation.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.operation.domain.DeviceComboPlanNumDO;
import com.yfp.operation.domain.DeviceCurrentComboPlanRoadDO;
import com.yfp.operation.domain.dto.CoordinateDTO;
import com.yfp.operation.domain.dto.DamageDTO;
import com.yfp.operation.domain.dto.OrderDetailDTO;
import com.yfp.operation.domain.vo.DeviceCurrentComboPlanRoadVO;
import com.yfp.operation.domain.vo.MachineVO;
import com.yfp.operation.domain.vo.OrderQueueVO;
import com.yfp.operation.service.DeviceComboPlanNumService;
import com.yfp.operation.domain.vo.*;
import com.yfp.operation.service.DeviceService;
import com.yfp.operation.domain.vo.OrderInfoVO;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备管理操作接口
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/device")
@Tag(name = "设备管理操作接口")
public class DeviceController extends BaseController {

    @Resource
    private DeviceService deviceService;
    @Resource
    private DeviceComboPlanNumService deviceComboPlanNumService;
    @Resource
    private WxMpService wxMpService;


    @Operation(summary = "投资人-设备详情-设备接口", description = "投资人-设备管理页面使用，展示当前登录者所管理的设备")
    @GetMapping("/investor/list")
    public R<List<MachineVO>> listDeviceInvestor(@Validated CoordinateDTO coordinateDTO) {
        return R.ok(deviceService.selectDeviceInvestorList(coordinateDTO));
    }

    @Operation(summary = "客服-获取设备列表", description = "设备管理页面使用，展示当前登录者所管理的设备")
    @GetMapping("/list")
    public R<List<MachineVO>> listDeviceOperation(@Validated CoordinateDTO coordinateDTO) {
        return R.ok(deviceService.selectDeviceOperationList(coordinateDTO));
    }

    @Operation(summary = "客服-获取设备详情", description = "设备管理页面使用")
    @GetMapping("/info")
    public R<MachineVO> info(@ApiParam(value = "设备id") String machineId) {
        return R.ok(deviceService.selectInfo(machineId));
    }

    @Operation(summary = "客服-排队订单列表", description = "设备详情页下面的排队订单列表")
    @GetMapping("/order/queue")
    public R<List<OrderQueueVO>> orderQueue(@ApiParam(value = "设备id") String machineId, @ApiParam(value = "手机号后四位") String phone) {
        return R.ok(deviceService.orderQueue(machineId, phone));
    }

    @Operation(summary = "客服-排队订单列表详情", description = "设备详情页下面的排队订单列表详情")
    @GetMapping("/order/info")
    public R<List<OrderInfoVO>> orderInfo(@ApiParam(value = "订单号") String orderId) {
        return R.ok(deviceService.orderInfo(orderId));
    }

    @Operation(summary = "客服-一键制作按钮", description = "设备详情页下面的排队订单列表的一键制作按钮")
    @PostMapping("/order/make")
    public R<Boolean> orderMake(@RequestBody List<OrderDetailDTO> orderDetailDTOS) {
        return R.ok(deviceService.orderOperationMake(orderDetailDTOS));
    }

    @Operation(summary = "客服-小程序代取", description = "小程序代取")
    @PostMapping("/order/min/make")
    public R<Boolean> orderMinMake(@ApiParam(value = "订单id") String orderId) {
        return R.ok(deviceService.orderMinMake(orderId));
    }

    @Operation(summary = "客服-报损数据获取", description = "补货管理-报损数据查询-查询当前的库存")
    @GetMapping("/damage")
    public R<List<DeviceCurrentComboPlanRoadVO>> currentRoadStockList(@ApiParam(value = "设备id") String machineId) {
        return R.ok(deviceService.currentRoadStockList(machineId));
    }

    @Operation(summary = "客服-报损数据提交", description = "补货管理-报损数据提交")
    @Log(title = "客服-报损数据提交", businessType = BusinessType.INSERT)
    @PutMapping("/damage")
    public R<Boolean> damageHandle(@RequestBody List<DamageDTO> damageDTOS) {
        return R.ok(deviceService.damageHandle(damageDTOS));
    }


    @Operation(summary = "客服-同步投放计划", description = "补货管理-同步投放计划数据")
    @GetMapping("/plan/synch")
    public R<Map<String, Object>> synchPlan(@RequestParam("machineId") @ApiParam(value = "machineId") String machineId) {
        List<DeviceCurrentComboPlanRoadVO> list = deviceService.submitPlan(machineId);
        DeviceComboPlanNumDO deviceComboPlanNumDO = deviceComboPlanNumService.getDayByMacineId(machineId);
        Map<String, Object> resultMap = new HashMap<>();
        // 投放计划
        resultMap.put("list", list);
        // 当天第一次投放时间
        resultMap.put("firstTime", null == deviceComboPlanNumDO ? "0" : deviceComboPlanNumDO.getCreateTime());
        return R.ok(resultMap);
    }


    @Operation(summary = "客服-提交同步计划", description = "补货管理-提交投放计划数据提交")
    @Log(title = "客服-提交投放计划", businessType = BusinessType.INSERT)
    @PostMapping("/plan")
    public R submitPlan(@RequestBody List<DeviceCurrentComboPlanRoadDO> comboList) {
        deviceService.deviceComboStockSync(comboList);
        return R.ok();
    }


    @Operation(summary = "客服-查看库存信息", description = "补货管理-查看库存信息")
    @GetMapping("/road")
    public R<List<DeviceCurrentComboPlanRoadDO>> roadInfo(@RequestParam("machineId") @ApiParam(value = "设备id") String machineId) {
        return R.ok(deviceService.roadInfo(machineId));
    }

    @Operation(summary = "客服-修改库存信息", description = "补货管理-修改库存信息")
    @Log(title = "客服-修改库存信息", businessType = BusinessType.UPDATE)
    @PutMapping("/road")
    public R updateRoad(@RequestBody List<DeviceCurrentComboPlanRoadDO> dccprs) {
        deviceService.updateRoad(dccprs);
        return R.ok();
    }


    /**
     * 获取配置信息
     *
     * @param url 当前网页的url
     * @return wx.config需要的配置参数
     */
    @GetMapping("/config")
    @Operation(summary = "扫一扫")
    @ResponseBody
    public R<WxJsapiSignature> config(String url) throws Exception {
        String decodeUrl = URLDecoder.decode(url, "UTF-8");
        logger.info("decodeUrl:{}", decodeUrl);
        WxJsapiSignature jsapiSignature = wxMpService.createJsapiSignature(decodeUrl);
        return R.ok(jsapiSignature);
    }


    @PostMapping("/verification")
    @Operation(summary = "核销实体券")
    public R<Void> verification(@RequestParam("ccId") Long ccId) {
        deviceService.verification(ccId);
        return R.ok();
    }
}
